{extend name="layout" /}
{block name="content"}
<div class="row">
  <div class="col-xs-3">
    <form id="searchForm">
      <div class="input-group input-group-sm demoTable">
        <input type="text" class="form-control input-sm" name="keywords" id="demoReload" autocomplete="off" placeholder="请输入关键字">
        <span class="input-group-btn">
          <button type="button" class="btn btn-info btn-flat layui-btn" data-type="reload">搜  索</button>
        </span>
      </div>
    </form>
  </div>
  <div class="col-xs-9" style="text-align: right">
    <button type="button" class="btn btn-success btn-sm cocoModal" isForm url="{:url('add')}" title="新增项目" fixat="1"><i class="fa fa-plus"></i> 新增</button>
    <button type="button" class="btn btn-info btn-sm export" title="内容表"><i class="fa fa-edit"></i> 数据导出</button>
  </div>
</div>
<table class="layui-table" lay-data="{height: 'full-75', cellMinWidth: 80, page: true, id:'lists',limit:30, url:'{:url('data_show_where',['order'=>'desc'])}'}" lay-filter="lists"> 
  <thead>
    <tr>
      <th lay-data="{type:'checkbox'}">ID</th>
      <th lay-data="{field:'id', sort: true, fixed: 'right', width:80, align:'center'}">ID</th>
      <th lay-data="{field:'title'}">标题</th>
      <th lay-data="{field:'catname'}">内容分类</th>
      <th lay-data="{field:'descrip'}">内容简介</th>
      <th lay-data="{field:'startdate'}">开始日期</th>
      <th lay-data="{field:'enddate'}">结束日期</th>
      <th lay-data="{field:'shares'}">分享次数</th>
      <th lay-data="{field:'addtime'}">添加时间</th>
      <th lay-data="{field:'status', width:85, templet: '#switchTpl', unresize: true}">状态</th>
      <th lay-data="{field:'top', width:110, templet: '#checkboxTpl', unresize: true}">置顶</th>
      <th lay-data="{fixed: 'right', width:200, align:'center', toolbar: '#barDemo'}">操作</th>
    </tr>
  </thead>
</table>
<script type="text/html" id="switchTpl">
  <!-- 这里的 checked 的状态只是演示 -->
  <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="checkboxTpl">
  <!-- 这里的 checked 的状态只是演示 -->
  <input type="checkbox" name="top" value="{{d.id}}" title="置顶" lay-filter="top" {{ d.top == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  {{# if({$uinfo.roleid} == 4){ }}
  <a class="layui-btn layui-btn-xs" lay-event="sel_dealer">选择经销商</a>
  {{# } }}
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
{/block}
{block name="script"}
<script type="text/javascript">
  function sel_dealer(data) {
    var layindex = layer.open({
      type: 2,
      area: ['1000px', '600px'],
      fixed: false, //不固定
      maxmin: true,
      content: APP_URL+"/admin/content/sel_dealer?id="+data.id,
      title:'选择经销商',
      btn: ['确定', '取消'],
      shade: [0.8, '#000000'],
      yes:function(index,layero){
        //parent.$('#parentIframe').text('我被改变了');
        var iframeWin = window[layero.find('iframe')[0]['name']];
        formobj = iframeWin.$("form");
        ajaxUrl = formobj.attr("action");
        if(ajaxUrl == undefined || ajaxUrl == ''){
          ajaxUrl = defaultSaveUrl;
        }
        datajson = formobj.serialize();
        searchMap.postData = datajson;
        $.post(ajaxUrl,searchMap,function(datajson){
          if(datajson.status == 1){
            layer.msg("操作成功",function(){
              layer.closeAll();
            });
          }
          else{
            layer.msg(datajson.msg);
            return false;
          }
        },'json');
      }
    });
  }
</script>
<script>
layui.use('table', function(){
  var table = layui.table;
    var $ = layui.$, active = {
      reload: function(){
        var demoReload = $('#demoReload');
        //执行重载
        table.reload('lists', {
          page: {
            curr: 1 //重新从第 1 页开始
          }
          ,where: $('#searchForm').serializeObject()
        });
      }
    };
    $('.demoTable .layui-btn').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  var form = layui.form;
  //监听性别操作
  form.on('switch(status)', function(obj){
    value = obj.elem.checked === true ? 1 : 0;
    var datajson = {id:this.value, field:this.name, value:value};
    $.post("{:url('set_field')}",datajson,function(datajson){
      var serializeObj={};
      if(datajson.status != 1){
        layer.msg(datajson.msg);
        return false;
      }
    },'json');
  });
  
  //监听锁定操作
  form.on('checkbox(top)', function(obj){
    value = obj.elem.checked === true ? 1 : 0;
    var datajson = {id:this.value, field:this.name, value:value};
    $.post("{:url('set_field')}",datajson,function(datajson){
      var serializeObj={};
      if(datajson.status != 1){
        layer.msg(datajson.msg);
        return false;
      }
    },'json');
  });
});
</script>
{/block}